/**
 * Copyright 2010 upTick Pty Ltd
 *
 * Licensed under the terms of the GNU Lesser General Public License version 3 
 * as published by the Free Software Foundation. You may obtain a copy of the
 * License at: http://www.gnu.org/copyleft/lesser.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations 
 * under the License. 
 */

package au.com.uptick.serendipity.client.sales.view;

import au.com.uptick.serendipity.client.sales.presenter.AccountsPresenter;
import au.com.uptick.serendipity.client.sales.view.handlers.AccountsUiHandlers;
import au.com.uptick.serendipity.client.widgets.StatusBar;
import au.com.uptick.serendipity.client.widgets.ToolBar;
import au.com.uptick.serendipity.shared.dto.AccountsDto;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;

import java.util.List;

public class AccountsView extends ViewWithUiHandlers<AccountsUiHandlers> implements AccountsPresenter.MyView {

    private static final String CONTEXT_AREA_WIDTH = "*";

    private final ToolBar toolbar;
    private final AccountsContextAreaListGrid listGrid;
    private final StatusBar statusBar;

    private VLayout panel;

    @Inject
    public AccountsView(ToolBar toolbar, AccountsContextAreaListGrid listGrid,
                        StatusBar statusBar) {

        Log.debug("AccountsView()");

        this.toolbar = toolbar;
        this.listGrid = listGrid;
        this.statusBar = statusBar;

        panel = new VLayout();

        // initialise the Accounts View layout container
        panel.setStyleName("crm-ContextArea");
        panel.setWidth(CONTEXT_AREA_WIDTH);

        // add the Tool Bar, List Grid, and Status Bar to the Accounts View layout container
        panel.addMember(this.toolbar);
        panel.addMember(this.listGrid);
        panel.addMember(this.statusBar);
        // this.addMember(this.jumpBar);

        bindCustomUiHandlers();
    }

    // as per AccountsRecord
    private static final String ACCOUNT_ID = "accountId";

    protected void bindCustomUiHandlers() {

        // register the ListGird handler's
        listGrid.addRecordDoubleClickHandler(new RecordDoubleClickHandler() {

            @Override
            public void onRecordDoubleClick(RecordDoubleClickEvent event) {
                Record record = event.getRecord();
                String accountId = record.getAttributeAsString(ACCOUNT_ID);

                Log.debug("onRecordDoubleClick() - " + accountId);

                if (getUiHandlers() != null) {
                    getUiHandlers().onRecordDoubleClicked(accountId);
                }
            }
        });
    }

    @Override
    public Widget asWidget() {
        return panel;
    }

    public void setServerResponse(List<AccountsDto> accountDtos) {
        // accountDtos == null when there are no items in table
        if (accountDtos != null) {
            listGrid.setServerResponse(accountDtos);
        }
    }

    public AccountsContextAreaListGrid getListGrid() {
        return listGrid;
    }

    public ToolBar getToolbar() {
        return toolbar;
    }
}

